package com.logicwin.RobotTravel.Screens;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.util.Log;

import com.logicwin.RobotTravel.IntroDialog;
import com.logicwin.RobotTravel.Main;

import com.logicwin.RobotTravel.R;
import com.logicwin.RobotTravel.Navigate.messages.BaseEvent;
import com.logicwin.RobotTravel.Services.IScreenService;
import com.logicwin.RobotTravel.Services.Impl.ServiceManager;
import com.logicwin.Views.IntroView;
import com.logicwin.Views.NavigateView;

public class ScreenIntro extends Screen implements Runnable {
	private final IScreenService screenService;
	private Thread mThread;
	private IntroView mView;
	private boolean mRunning = true;
	public ScreenIntro() {
		super(SCREEN_TYPE.INTRO_T, ScreenKtv.class.getCanonicalName());
		screenService = ServiceManager.getScreenService();
	}

	static String TAG = ScreenNavigate.class.getCanonicalName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_intro);
		mView = (IntroView)this.findViewById(R.id.IntroView);   
	}

	public boolean onTouchEvent(MotionEvent event) {
		Log.i(TAG, "MobileMenuActivity::onTouchEvent is catched.");

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			float xPos = event.getX();
			float yPos = event.getY();
        	if(ServiceManager.getPositoinService().hitUndo((int)xPos, (int)yPos)){            		
        		onPause();
        		ServiceManager.getScreenService().back();
        	}   			
        	xPos = xPos*800/Main.screenWidth;
        	yPos = yPos*480/Main.screenHeight;			
			int idx = ServiceManager.getPositoinService().hitPosition(
					(int) xPos, (int) yPos);
			if (idx >= 0) {
				Intent intent = new Intent(this, IntroDialog.class);
				idx++;
				intent.putExtra("sceen", "sceen" + idx);
				Log.i(TAG, "screen"+idx +" is invoked");
				this.startActivity(intent);
				return true;
			}
			return true;
		case MotionEvent.ACTION_UP:

			return true;

		}
		return super.onTouchEvent(event);
	}
    public void run() {  
        while (mRunning) {  
            try {  
            	
            	Thread.sleep(100); 
	            mView.postInvalidate();
	            
            } catch (InterruptedException e) {  
                Thread.currentThread().interrupt();  
            }    
        }  
    }  	
	protected void onResume()
	{	
		//		mThread.resume();
		Log.v(TAG, "onResume()");
		super.onResume();
	}

	protected void onStart()
	{
		mThread = new Thread(this); 
		mRunning = true;
		mThread.start();
		Log.v(TAG, "onStart()");
		super.onStart();
	}	
	@Override
	public void onStop() {
		mRunning = false;
		Log.v(TAG, "onStop()");
		super.onStop();
	}	
	@Override
	protected void onPause() {
		//		mThread.suspend();
		// TODO Auto-generated method stub
		Log.v(TAG, "onPause()");
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.v(TAG, "onDestroy()");
		super.onDestroy();
	}		
}
